package com.framework2.util;

import android.app.Fragment;
import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.yaxin.yyt.R;

public class GlideUtil {
    private static volatile GlideUtil singleton;

    private GlideUtil() {
    }

    public static GlideUtil getInstance() {
        if (singleton == null) {
            synchronized (GlideUtil.class) {
                if (singleton == null) {
                    singleton = new GlideUtil();
                }
            }
        }
        return singleton;
    }

    public void showRemoteImage(Context context, Priority priority, int placeholder, int error, DiskCacheStrategy
            diskCacheStrategy, String imgUrl, ImageView imageView) {
        try {
            Glide.with(context).setDefaultRequestOptions(RequestOptions.priorityOf(priority)
                    .placeholder(placeholder)
                    .error(error).diskCacheStrategy(diskCacheStrategy))
                    .load(imgUrl)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showRemoteImage(Fragment fragment, Priority priority, int placeholder, int error, DiskCacheStrategy
            diskCacheStrategy, String imgUrl, ImageView imageView) {
        try {
            Glide.with(fragment).setDefaultRequestOptions(RequestOptions.priorityOf(priority)
                    .placeholder(placeholder)
                    .error(error).diskCacheStrategy(diskCacheStrategy))
                    .load(imgUrl)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLocalImage(Context context, Priority priority, DiskCacheStrategy
            diskCacheStrategy, int resId, ImageView imageView) {
        try {
            Glide.with(context).setDefaultRequestOptions(RequestOptions.priorityOf(priority)
                    .diskCacheStrategy(diskCacheStrategy))
                    .load(resId)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLocalImage(Fragment fragment, Priority priority, DiskCacheStrategy
            diskCacheStrategy, int resId, ImageView imageView) {
        try {
            Glide.with(fragment).setDefaultRequestOptions(RequestOptions.priorityOf(priority)
                    .diskCacheStrategy(diskCacheStrategy))
                    .load(resId)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>*/
    /*===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>===>>*/
    public void showRemoteImage(Context context, String imgUrl, ImageView imageView) {
        showRemoteImage(context, Priority.HIGH, R.mipmap.default_icon, R.mipmap.default_icon, DiskCacheStrategy.ALL,
                imgUrl, imageView);
    }

    public void showRemoteImage(Fragment fragment, String imgUrl, ImageView imageView) {
        showRemoteImage(fragment, Priority.HIGH, R.mipmap.default_icon, R.mipmap.default_icon, DiskCacheStrategy.ALL,
                imgUrl, imageView);
    }

    public void showLocalImage(Context context, int resId, ImageView imageView) {
        showLocalImage(context, Priority.HIGH, DiskCacheStrategy.ALL, resId, imageView);
    }

    public void showLocalImage(Fragment fragment, int resId, ImageView imageView) {
        showLocalImage(fragment, Priority.HIGH, DiskCacheStrategy.ALL, resId, imageView);
    }
}
